package com.qk.common.entity;

import com.qk.common.enums.AvatarStatus;

import java.time.LocalDateTime;

public class AvatarBuilder {
    private Long userId;
    private String filePath;
    private String fileMd5;
    private Integer isGarbage = AvatarStatus.ACTIVE.getCode();
    private LocalDateTime createTime = LocalDateTime.now();
    private LocalDateTime markTime;
    
    public static AvatarBuilder builder() {
        return new AvatarBuilder();
    }
    
    public AvatarBuilder userId(Long userId) {
        this.userId = userId;
        return this;
    }
    
    public AvatarBuilder filePath(String filePath) {
        this.filePath = filePath;
        return this;
    }
    
    public AvatarBuilder fileMd5(String fileMd5) {
        this.fileMd5 = fileMd5;
        return this;
    }
    
    public AvatarBuilder status(AvatarStatus status) {
        this.isGarbage = status.getCode();
        if (status == AvatarStatus.GARBAGE) {
            this.markTime = LocalDateTime.now();
        }
        return this;
    }
    
    public AvatarBuilder markAsGarbage() {
        return status(AvatarStatus.GARBAGE);
    }
    
    public AvatarBuilder markAsActive() {
        return status(AvatarStatus.ACTIVE);
    }
    
    // 注意：这个方法需要在有Avatar类的模块中调用
    public <T> T build(java.util.function.Supplier<T> avatarSupplier, 
                       java.util.function.BiConsumer<T, Long> userIdSetter,
                       java.util.function.BiConsumer<T, String> filePathSetter,
                       java.util.function.BiConsumer<T, String> fileMd5Setter,
                       java.util.function.BiConsumer<T, Integer> isGarbageSetter,
                       java.util.function.BiConsumer<T, LocalDateTime> createTimeSetter,
                       java.util.function.BiConsumer<T, LocalDateTime> markTimeSetter) {
        T avatar = avatarSupplier.get();
        userIdSetter.accept(avatar, userId);
        filePathSetter.accept(avatar, filePath);
        fileMd5Setter.accept(avatar, fileMd5);
        isGarbageSetter.accept(avatar, isGarbage);
        createTimeSetter.accept(avatar, createTime);
        markTimeSetter.accept(avatar, markTime);
        return avatar;
    }
}